<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/

/**
 * Get all posts
 * @returns array
 * @return array of zero or more subscriptions, or NULL on failure
 * @param tagid interger tag id 
 */
function conversations_userapi_gettagid_bypost_search($args)
{
    extract($args);

    // Optional arguments

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $cposts = $xartable['cposts'];
    $clook  = $xartable['clook'];
   
    $query = "SELECT $clook.xar_tagid"
            . " FROM $clook "
            . " LEFT JOIN $cposts "
            . " ON $cposts.xar_postid = $clook.xar_postid "
            //. " WHERE MATCH($cposts.xar_text) AGAINST ('$q') " 
            . " WHERE $cposts.xar_text LIKE ? "
            . " ORDER by $clook.xar_tagid";

    $where = array();
    $bind = array();

    $result =& $dbconn->SelectLimit($query,  100, 0, $q);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($tagid) = $result->fields;
        
        $tags[] = array(
                'tagid'     => $tagid // Might as well have it in the array.  No telling when it might be useful
        );
    }
    $result->Close();
    //return var_export($tags);
    
    // Look for matching tag in tags as well.
    
    $ctags = $xartable['ctags'];    
    
    $query = "SELECT xar_id, xar_tag, xar_timestamp,"
            . " xar_lastvisit, xar_status, xar_uid, xar_count, "
            . " xar_options "
            . " FROM $ctags ";
    /*        
    if (!empty($q)) {
        $where[] = " MATCH(xar_tag) AGAINST ('$q') ";
        //$bind[] = (int)$tag['tagid'];
    }  
    */      
    $where = array();
    $bind = array();
    
    foreach ($tags as $tag){

        if (!empty($tagid)) {
            $where[] = ' xar_id = ? ';
            $bind[] = (int)$tag['tagid'];
        }
    }
    
    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' OR ', $where);
    } else {
        $query .= ' WHERE xar_tag LIKE ? ';
        $bind[] = $q;
    }
    
    
    $result =& $dbconn->SelectLimit($query, 100, 0, $bind);
    if (!$result) return;

    $search = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($id, $tag, $timestamp, $lastvisit, $status, $uid, $count, $options) = $result->fields;

        $search[] = array(
                'id'        => $id,
                'tag'       => xarVarPrepForDisplay($tag),
                'timestamp' => $timestamp,
                'lastvisit' => $lastvisit,
                'status'    => $status,
                'uid'       => $uid,
                'count'     => $count//,
                //'options'   => unserialize($options)
        );
    }

    $result->Close();
    return $search;
}
?>